<?php
include("../includes/DBFunctions.php");
include("../includes/getCredentials.php");

function animalYearlyReportFunctor($sid) {
	$db = new DBFunctions;
	$gc = new GetCredentials;
	$uid = $gc->getUserID();
	$db->connectDB();
	$query = "call getLog_Animal(".$sid.",".$uid.",@a);";
	$result = $db->query($query);
	if( !$result ) {
		die("alert('Failed stored procedure call (getLog_Animal)');");
	}
	if (mysqli_num_rows($result) < 1) {
		$db->closeDB();
		$toPrint = "<div><h3>n/a</h3></div>";
	} else {
		$toPrint = "";
		while( $line = mysqli_fetch_array( $result, MYSQL_ASSOC ) ) {
			$onclick = "setTitle(\\\"<h2>Region: ".$line['Region Name']."</h2>\\\");var s=new Array(\\\"rid=".$line['Region_ID']."\\\");call(\\\"functors/regionReportFunctor.php\\\",s,1,0);";
			$toPrint .= "<div><h3><a href='#' onclick='".$onclick."'>".$line['Region Name']."</a></h3><ul>";
			$toPrint .= "<li>".$line['Old Population']." &rarr; ".$line['New Population']."</li>";
			$toPrint .= "</ul></div>";
		}
		$db->closeDB();
	}
	
	echo 'displayReport("'.$toPrint.'");';
}

if( !isset($_POST['sid']) || $_POST['sid'] == '') {
	die("alert('No species was selected. Please try again.');");
}
$sid = $_POST['sid'];
animalYearlyReportFunctor($sid);
?>